package org.handsomezhi.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.handsomezhi.shortlink.admin.common.convention.exception.ClientException;
import org.handsomezhi.shortlink.admin.common.convention.result.Result;
import org.handsomezhi.shortlink.admin.common.convention.result.Results;
import org.handsomezhi.shortlink.admin.common.enums.UserErrorCodeEnum;
import org.handsomezhi.shortlink.admin.dto.req.UserLoginReqDTO;
import org.handsomezhi.shortlink.admin.dto.req.UserRegisterReqDTO;
import org.handsomezhi.shortlink.admin.dto.req.UserUpdateReqDTO;
import org.handsomezhi.shortlink.admin.dto.resp.UserActualRespDTO;
import org.handsomezhi.shortlink.admin.dto.resp.UserLoginRespDTO;
import org.handsomezhi.shortlink.admin.dto.resp.UserRespDTO;
import org.handsomezhi.shortlink.admin.service.UserService;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据同户名查询用户信息
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result getUserByUsername(@PathVariable("username") String username){
        UserRespDTO result = userService.getUserByUsername(username);
        if(result == null){
            throw new ClientException(UserErrorCodeEnum.USER_NULL);
        }else {
            return Results.success(result);
        }
    }

    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result getActualUserByUsername(@PathVariable("username") String username){
        UserRespDTO result = userService.getUserByUsername(username);
        if(result == null){
            throw new ClientException(UserErrorCodeEnum.USER_NULL);
        }else {
            UserActualRespDTO bean = BeanUtil.toBean(result, UserActualRespDTO.class);
            return Results.success(bean);
        }
    }


    /**
     * 查看用户名是否已存在
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 注册用户
     * @param registerReqDTO 注册参数
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO registerReqDTO){
        userService.register(registerReqDTO);
        return Results.success();
    }


    /**
     * 用户信息修改接口
     * @param userUpdateReqDTO
     * @return
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO userUpdateReqDTO){
        userService.update(userUpdateReqDTO);
        return Results.success();
    }

    /**
     * 用户登录接口
     * @param userLoginReqDTO
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO userLoginReqDTO){
        UserLoginRespDTO result = userService.login(userLoginReqDTO);
        return Results.success(result);
    }

    /**
     * 检查用户是否登录
     * @param username
     * @param token
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username")String username,
                                      @RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }


    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,
                               @RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }

}
